package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.*;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.FillingCardRechargeService;
import com.gljx.web.service.FillingCardService;
import com.gljx.web.service.RefuelCardRegEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class FuelCardJournalController {

	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private FillingCardService fillingCardService;
	
	@Autowired
	private RefuelCardRegEntryService refuelCardRegEntryService;
	
	@Autowired
	private FillingCardRechargeService fillingCardRechargeService;
	
	/**
	 * 进入选择加油卡报表条件
	 * @return
	 * @createTime 2017-12-11 10:46 am
	 * @author zyq.
	 */
	@RequestMapping(value="/chooseFuelCardJournal")
	public String chooseFuelCardJournal(HttpServletRequest request){
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");                                   //获取已经登录用户数据
		Department department=departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
		Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
		  		 cale.add(Calendar.MONTH, 0);
		  		 cale.set(Calendar.DAY_OF_MONTH, 1);
		Date resultDate = cale.getTime(); // 结果
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String beginTime = sdf.format(resultDate);
		String endTime = sdf.format(new Date());
		Map<String , Object> map = new HashMap<>();
							 map.put("fid",null);
							 map.put("beginTime", beginTime);
							 map.put("endTime", endTime);
							 map.put("comapnyId", department.getFid());
	    List<FillingCard> fillingCards = fillingCardService.queryFillingCard(user.getFcompanyid());  //把数据传到页面
		request.setAttribute("fillingCards", fillingCards);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("company", department.getFnameL2());
		return "backHome/journaling/cardJournaling/fuelCardJournal";	
	}
	
	
	@RequestMapping(value="/selFuelCardJournalByName")
	@ResponseBody
	public List<FillingCard> selFuelCardJournalByName(HttpServletRequest request,String name){
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");                                   //获取已经登录用户数据
		Department department=departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
		List<FillingCard> fillingCards = fillingCardService.selectFillingCardByfnameL2(department.getFid(), name);
		return fillingCards;
	}
	
	
	/**
	 * 根据fid查询加油卡记录
	 * @param request
	 * @param fid
	 * @return
	 */
	@RequestMapping(value="/selFuelCardJournalById")
	@ResponseBody
	public FillingCard selFuelCardJournalById(HttpServletRequest request,String fid){
		FillingCard fillingCards = fillingCardService.selectByPrimaryKey(fid);
		return fillingCards;
	}
	
	@RequestMapping(value="/selFuelCardJournal")
	public String selFuelCardJournal(HttpServletRequest request,String beginTime,String endTime,String fname,String fid) {
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");                                   //获取已经登录用户数据
		Department department=departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
		Map<String , Object> map = new HashMap<>();
							 map.put("fid",fid);
							 map.put("beginTime", beginTime);
							 map.put("endTime", endTime);
							 map.put("comapnyId", department.getFid());
		List<RefuelCardRegEntry> cardRegEntries = refuelCardRegEntryService.selRefuelCardRegEntry(map);
		List<FillingCardRecharge> fillingCardRecharges = fillingCardRechargeService.selFillingCardRecharge(map);
		Double fillingBeginBalance = fillingCardRechargeService.selFillingCardBeginBalance(map); 	//期初加油卡
		Double reflueBeginBalance = refuelCardRegEntryService.selFeflueBeginBalance(map);			//期初车辆加油
		Double beginBalance = null ;
		if(fillingBeginBalance == null || reflueBeginBalance == null ){
			beginBalance = 0.0 ;
		}else{
			beginBalance = fillingBeginBalance-reflueBeginBalance;
			BigDecimal   beg = new   BigDecimal(beginBalance);  
				   beginBalance = beg.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	   
		}
		Double fillingBeginBalances = fillingCardRechargeService.selFillingCardBeginBalances(map); //期间加油卡
		Double reflueBeginBalances = refuelCardRegEntryService.selFeflueBeginBalances(map);		  //期间车辆加油
		Double endBalance = null;
		if(fillingBeginBalances!=null & reflueBeginBalances!=null){
		      endBalance = beginBalance+fillingBeginBalances-reflueBeginBalances;
		BigDecimal   end = new   BigDecimal(endBalance);  
			   endBalance = end.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	
		}else if(fillingBeginBalances!=null){
		    endBalance = beginBalance+fillingBeginBalances;
			BigDecimal   end = new   BigDecimal(endBalance);  
			endBalance = end.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	
		}else{
			endBalance = beginBalance;
			if(endBalance == null){

			}else {
				BigDecimal end = new BigDecimal(endBalance);
				endBalance = end.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			}
		}
	    Double cfamout = refuelCardRegEntryService.selFeflueCfamout(map);
	    List<FillingCard> fillingCards = fillingCardService.queryFillingCard(user.getFcompanyid());  //把数据传到页面
		request.setAttribute("fillingCards", fillingCards);
	    request.setAttribute("cfamout", cfamout);
		request.setAttribute("fname", fname);
		request.setAttribute("fid", fid);
		request.setAttribute("beginBalance", beginBalance);
		request.setAttribute("reflueBeginBalances", reflueBeginBalances);
		request.setAttribute("fillingBeginBalances", fillingBeginBalances);
		request.setAttribute("endBalance", endBalance);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("company", department.getFnameL2());
		request.setAttribute("cardRegEntries", cardRegEntries);
		request.setAttribute("fillingCardRecharges", fillingCardRecharges);
		return "backHome/journaling/cardJournaling/fuelCardJournal";	
	}
	
	
	
	
}
